package com.example.chirdrenserver.controller.AdminController;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.example.chirdrenserver.Authority.Authority;
import com.example.chirdrenserver.Holder.UserHolder;
import com.example.chirdrenserver.Pojo.dto.PermissionDTO;
import com.example.chirdrenserver.Pojo.entity.Permission;
import com.example.chirdrenserver.Service.AdminService.PermissionService;
import com.example.chirdrenserver.Utils.Result;
import com.example.chirdrenserver.exception.BaseException;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: Yu
 * @CreateTime: 2025-06-25
 * @Description:
 */
@RestController
@RequestMapping("/admin/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    /**
     * 初始化菜单
     *
     * @return
     */
    @GetMapping("/initMenu")
    public Result initMenu() {

        if (!permissionService.initMenu( UserHolder.get())) {
            throw new BaseException("PREMISSION_ERROR");
        }
        return Result.success();
    }

    @GetMapping
    @Authority(authority= "permission:permission")
    public Result<List<Permission>> getPermission() {
        List<Permission> permissions = permissionService.list();
        return Result.success(permissions);
    }

    @GetMapping("/info/{id}")
    @Authority(authority= "permission:permission")
    public Result getPermissionInfo(@PathVariable Integer id) {
        Permission permissions = permissionService.getById(id);
        return Result.success(permissions);
    }

    @DeleteMapping("/{id}")
    @Authority(authority= "permission:permission")
    public Result deletePermission(@PathVariable List<Integer> id) {
        permissionService.deleteByids(id);
        return Result.success();
    }

    @PutMapping("/{id}")
    @Authority(authority= "permission:permission")
    public Result updatePermission(@PathVariable Integer id, @RequestBody PermissionDTO permissionDTO) {
        permissionService.updateByid(id, permissionDTO);
        return Result.success();
    }

    @PostMapping()
    @Authority(authority= "permission:permission")
    public Result addPermission(@RequestBody PermissionDTO permissionDTO) {
        permissionService.add(permissionDTO);
        return Result.success();
    }
}